<template>
  <div class="pptist-editor">
    <EditorHeader class="layout-header" />
    <div class="layout-content">
      <Thumbnails class="layout-content-left" />
      <div class="layout-content-center">
        <CanvasTool class="center-top" />
        <Canvas class="center-body" :style="{ height: `calc(100% - ${remarkHeight + 40}px)` }" />
        <Remark
          class="center-bottom" 
          v-model:height="remarkHeight" 
          :style="{ height: `${remarkHeight}px` }"
        />
        <Toolbar class="layout-content-right post-right"  v-if="showShadowPannel || backFill || ChartStyleShow"/>
      </div>
      <!-- <Toolbar class="layout-content-right" /> -->
    </div>
    <div class="scrren-bottom">
          <div class="right-handler">
            <IconMinus class="handler-item viewport-size" @click="scaleCanvas('-')" />
            <Popover trigger="click" v-model:visible="canvasScaleVisible">
              <template #content>
                <div class="viewport-size-preset">
                  <div 
                    class="preset-item" 
                    v-for="item in canvasScalePresetList" 
                    :key="item" 
                    @click="applyCanvasPresetScale(item)"
                  >{{item}}%</div>
                </div>
              </template>
              <span class="text">{{canvasScalePercentage}}</span>
            </Popover>
            <IconPlus class="handler-item viewport-size" @click="scaleCanvas('+')" />
            <Tooltip :mouseLeaveDelay="0" :mouseEnterDelay="0.5" title="适应屏幕">
              <IconFullScreen class="handler-item viewport-size-adaptation" @click="resetCanvas()" />
            </Tooltip>
          </div>
    </div>
  </div>

  <Modal
    :visible="!!dialogForExport" 
    :footer="null" 
    centered
    :closable="false"
    :width="680"
    destroyOnClose
    @cancel="closeExportDialog()"
  >
    <ExportDialog />
  </Modal>
</template>

<script lang="ts" setup>
import { ref, watch } from 'vue'
import { storeToRefs } from 'pinia'
import { useMainStore } from '@/store'
import useGlobalHotkey from '@/hooks/useGlobalHotkey'
import usePasteEvent from '@/hooks/usePasteEvent'
import useScaleCanvas from '@/hooks/useScaleCanvas'
import EditorHeader from './EditorHeader/index.vue'
import Canvas from './Canvas/index.vue'
import CanvasTool from './CanvasTool/index.vue'
import Thumbnails from './Thumbnails/index.vue'
import Toolbar from './Toolbar/index.vue'
import Remark from './Remark/index.vue'
import ExportDialog from './ExportDialog/index.vue'
import { PPTElementShadow } from '@/types/slides'
const mainStore = useMainStore()
const { dialogForExport, handleElement, showShadowPannel, backFill, ChartStyleShow } = storeToRefs(mainStore)
const closeExportDialog = () => mainStore.setDialogForExport('')
const shadow = ref<PPTElementShadow>()
const {
  scaleCanvas,
  setCanvasScalePercentage,
  resetCanvas,
  canvasScalePercentage,
} = useScaleCanvas()
const remarkHeight = ref(40)
const canvasScalePresetList = [200, 150, 100, 80, 50]
const canvasScaleVisible = ref(false)
// watch(handleElement, () => {
//   if (!handleElement.value) return
//   shadow.value = 'shadow' in handleElement.value ? handleElement.value.shadow : undefined
// }, { deep: true, immediate: true })
const applyCanvasPresetScale = (value: number) => {
  setCanvasScalePercentage(value)
  canvasScaleVisible.value = false
}
useGlobalHotkey()
usePasteEvent()
</script>

<style lang="scss" scoped>
.pptist-editor {
  height: 100%;
}
.layout-header {
  height: 40px;
}
.layout-content {
  height: calc(100% - 40px);
  display: flex;
  padding-bottom: 30px;
}
.layout-content-left {
  width: 160px;
  height: 100%;
  flex-shrink: 0;
}
.layout-content-center {
  width: calc(100% - 160px);
  .center-top {
    height: 76px;
    align-items: center;
  }
}
.layout-content-right {
  width: 260px;
  height: 100%;
}
.post-right {
  position: absolute;
  right: 0;
  top: 40px;
  z-index: 9;
}
.scrren-bottom {
  width: 100%;
  height: 30px;
  background: white;
  position: fixed;
  bottom: 0;
  z-index: 10;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  .right-handler {
    display: flex;
    align-items: center;
    margin-right: 20px;
    span {
      margin-right: 10px;
      cursor: pointer;
    }
    .text {
      width: 40px;
      text-align: center;
      cursor: pointer;
      margin-top: 2px;
    }
    .viewport-size {
      font-size: 13px;
    }
  }
}
</style>